﻿using ML.Entity;
using ML.IBusiness;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 用户管理控制器
    /// </summary>
    [Login]
    public class UserController : BaseController
    {
        private readonly IUserBusiness userBusiness;
        private readonly IReceivingAddressBusiness receivingAddressBusiness;
        private readonly IUserInvitationBusiness userInvitationBusiness;
        public UserController(IUserBusiness _userBusiness, IReceivingAddressBusiness _receivingAddressBusiness, IUserInvitationBusiness _userInvitationBusiness)
        {
            userBusiness = _userBusiness;
            receivingAddressBusiness = _receivingAddressBusiness;
            userInvitationBusiness = _userInvitationBusiness;
        }

        /// <summary>
        /// 用户管理登录页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.InitInfo = "用户管理";
            return View();
        }

        /// <summary>
        /// 根据查询条件查询用户信息
        /// </summary>
        /// <param name="userSearch">用户查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult List(UserSearch userSearch)
        {
            var data = userBusiness.List(userSearch);
            return JsonDateTime(data.data);
        }

        /// <summary>
        /// 根据用户Id查询用户信息
        /// </summary>
        /// <param name="Id">用户Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetById(string Id)
        {
            var data = userBusiness.GetById(Id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 根据用户Id查询用户的收货地址
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetAddRessByUserId(string id)
        {
            var data = receivingAddressBusiness.GetAddRessByUserId(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 根据用户Id查询用户的邀请人信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetUserInvitationByUserId(string id)
        {
            var data = userInvitationBusiness.GetUserInvitationByUserId(id);
            return JsonDateTime(data);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Add(UserEntity entity)
        {
            var data = userBusiness.Add(entity);
            return Json(data);
        }

        /// <summary>
        /// 修改用户分类
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <param name="user_type">用户分类</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult UpdateUserType(string id, int user_type)
        {
            var data = userBusiness.UpdateUserType(id, user_type);
            return Json(data);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteUser(string id)
        {
            var data = userBusiness.Delete(id);
            return Json(data);
        }
    }
}